package oa.hleast.xswl.controller.admin.chat;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.form.chat.CmsChatAutoReplyForm;
import oa.hleast.xswl.pojo.vo.chat.CmsChatAutoReplyVo;
import oa.hleast.xswl.pojo.vo.chat.CmsChatMessageLogVo;
import oa.hleast.xswl.service.chat.IChatService;
import oa.hleast.xswl.service.chat.impl.ChatAutoReplyServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

@Api(tags = "【系统管理】智能客服")
@RestController("ChatController")
@RequestMapping("/api.admin/v1/chat")
@Slf4j
@AllArgsConstructor
public class ChatController {

    private final ChatAutoReplyServiceImpl autoReplyService;
    private final IChatService iChatService;

    @ApiOperation(value = "聊天记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, paramType = "query", dataType = "Integer",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Integer",dataTypeClass = Integer.class)
    })
    @GetMapping("/findChatMessageLog")
    private Result<List<CmsChatMessageLogVo>> findChatMessageLog(@RequestParam @NotNull Integer page, @RequestParam @NotNull Integer limit, Integer userId) {
        Page<CmsChatMessageLogVo> result = iChatService.findChatMessageLog(userId.longValue(), page, limit);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "新增匹配库", httpMethod = "POST")
    @ApiImplicitParam(name = "chatAutoReplyForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsChatAutoReplyForm",dataTypeClass = CmsChatAutoReplyForm.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody CmsChatAutoReplyForm chatAutoReplyForm) {
        boolean status = autoReplyService.add(chatAutoReplyForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "修改匹配库", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "chatAutoReplyForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsChatAutoReplyForm",dataTypeClass = CmsChatAutoReplyForm.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull CmsChatAutoReplyForm chatAutoReplyForm) {

        if ((int) id != chatAutoReplyForm.getId()) {
            throw new BizException("数据ID不一致");
        }
        boolean status = autoReplyService.updateReply(chatAutoReplyForm);
        return Result.judge(status);
    }

    @ApiOperation(value = "匹配库列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 page分页查询", required = true, paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("query-list")
    public Result<List<CmsChatAutoReplyVo>> list(
            @RequestParam @NotBlank String queryMode,
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit
    ) {

        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsChatAutoReplyVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                result = autoReplyService.pageListByApps(page, limit);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "匹配库详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsChatAutoReplyVo> detail(@PathVariable Long id) {
        CmsChatAutoReplyVo advert = autoReplyService.detail(id);
        return Result.success(advert);
    }

    @ApiOperation(value = "删除匹配库", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") String ids) {
        boolean status = autoReplyService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }
}
